﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

using Library.Bll.Account;

namespace Yeex.Boss.Web.Helper
{
    public static class ButtonHelper
    {
        /// <summary>
        /// 根据用户权限创建操作按钮（如果该用户无该页面的操作权限，则不创建任何按钮）；
        /// 每个按钮的Level值是level的值，该值必须为非负整数（创建时，请尽量使用较小的数）；
        /// 没有Level值的按钮在当前页面有操作权限时会被创建。
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="buttons">操作按钮</param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper htmlHelper, params IDictionary<string, object>[] buttons)
        {
            if (buttons == null)
            {
                return null;
            }

            string controllerName = htmlHelper.ViewContext.RouteData.Values["controller"].ToString();
            string actionName = htmlHelper.ViewContext.RouteData.Values["action"].ToString();
            long operateLevel = FunctionBLL.GetOperateLevel(controllerName, actionName);
            if (operateLevel <= 0)
            {
                //用户无操作权限，不创建任何按钮
                return null;
            }

            StringBuilder html = new StringBuilder();
            foreach (IDictionary<string, object> d in buttons)
            {
                if (d.ContainsKey("level"))
                {
                    int level = 0;
                    if (!int.TryParse(d["level"].ToString(), out level))
                    {
                        throw new ArgumentException("button的level属性必须为数字。");
                    }

                    if (level < 0)
                    {
                        throw new ArgumentOutOfRangeException("button的level属性必须为非负整数。");
                    }

                    if (!FunctionBLL.CanOperate(level, operateLevel))
                    {
                        //无该按钮的操作权限
                        continue;
                    }
                    d.Remove("level");
                }

                string name = null;
                if (d.ContainsKey("name"))
                {
                    name = d["name"].ToString();
                    d.Remove("name");
                }
                string text = null;
                if (d.ContainsKey("text"))
                {
                    text = d["text"].ToString();
                    d.Remove("text");
                }
                string type = null;
                if (d.ContainsKey("type"))
                {
                    type = d["type"].ToString();
                    d.Remove("type");
                }
                html.Append(htmlHelper.Button(name, text, type, d));
            }

            return new MvcHtmlString(html.ToString());
        }

        /// <summary>
        /// 根据用户权限创建操作按钮（如果该用户无该页面的操作权限，则不创建任何按钮）；
        /// 每个按钮的Level值是level的值，该值必须为非负整数（创建时，请尽量使用较小的数）；
        /// 没有Level值的按钮在当前页面有操作权限时会被创建。
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="buttons"></param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper htmlHelper, params object[] buttons)
        {
            List<IDictionary<string, object>> bs = new List<IDictionary<string, object>>();
            foreach (object o in buttons)
            {
                bs.Add(HtmlHelper.AnonymousObjectToHtmlAttributes(o));
            }

            return htmlHelper.Button(bs.ToArray());
        }

        /// <summary>
        /// 创建操作按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">name</param>
        /// <param name="text">显示的文字</param>
        /// <param name="type">按钮类型（button、submit、reset）</param>
        /// <param name="htmlAttributes">HTML属性</param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper htmlHelper, string name, string text, string type, IDictionary<string, object> htmlAttributes)
        {
            bool flag = false;
            string fullHtmlFieldName = name;
            if (!string.IsNullOrEmpty(fullHtmlFieldName))
            {
                fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
                if (!string.IsNullOrEmpty(fullHtmlFieldName))
                {
                    flag = true;
                }
            }
            if (string.IsNullOrEmpty(type))
            {
                type = "button";
            }
            TagBuilder tagBuilder = new TagBuilder("button");
            if (flag)
            {
                tagBuilder.GenerateId(fullHtmlFieldName);
            }
            tagBuilder.MergeAttributes<string, object>(htmlAttributes, true);
            tagBuilder.MergeAttribute("type", type);
            if (flag)
            {
                tagBuilder.MergeAttribute("name", fullHtmlFieldName, true);
            }
            tagBuilder.SetInnerText(text);

            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// 创建操作按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">name</param>
        /// <param name="text">显示的文字</param>
        /// <param name="type">按钮类型（button、submit、reset）</param>
        /// <param name="htmlAttributes">HTML属性</param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper htmlHelper, string name, string text, string type, object htmlAttributes)
        {
            return htmlHelper.Button(name, text, type, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
    }
}